const axios = require('../../../utils/request')
Page({
    data: {
        typeIndex:1,     //1=微信号，2=二维码
        qrcodeImage:'',
        name:'',
        id:'',
        remark:''
    },
    onLoad(e) {
        this.setData({id:e.id})
        if(this.data.id){
            wx.showLoading({
                title:'加载中...',
                mask:true
            })
            this.getProviderDetail()
        }
    },
    selectType(e){
        let index = e.currentTarget.dataset.index
        this.setData({typeIndex:index})
    },
    onSelectQrcodeImage(){
        wx.chooseImage({
            count:1,
            success:(res)=>{
                if(res.errMsg == 'chooseImage:ok'){
                    wx.showLoading({
                        mask:true,
                        title:'上传中...'
                    })
                    axios.upLoadImage(res.tempFilePaths[0]).then(res=>{
                        wx.hideLoading()
                        if(res.code==1){
                            this.setData({qrcodeImage:res.image})
                        }
                    })
                }
                
            }
        })
    },
    onSubmit(){
        if(this.data.typeIndex==1){
            if(this.data.name==''){
                wx.showToast({
                    title:'请输入微信号',
                    icon:'none'
                })
                return
            }
        }
        if(this.data.typeIndex==2){
            if(this.data.qrcodeImage==''){
                wx.showToast({
                    title:'请上传二维码',
                    icon:'none'
                })
                return
            }
        }
        wx.showLoading({
            mask:true,
            title:'加载中...'
        })
        axios.post(this.data.id?'/api/contact/edit':'/api/contact/add',{
            type:this.data.typeIndex,
            wechat:this.data.name,
            qrcode:this.data.qrcodeImage,
            id:this.data.id,
            remark:this.data.remark
        }).then(res=>{
            wx.hideLoading()
            if(res.code==1){
                wx.navigateBack({
                    detal:1
                })
            }else{
                wx.showtOast({
                    icon:'none',
                    title:res.msg
                })
            }
        })
    },
    onDelete(){
        axios.post('/api/providers/del',{
            id:this.data.id
        }).then(res=>{
            if(res.code==1){
                wx.navigateBack({
                    delta:1
                })
            }else{
                wx.showtoast({
                    title:res.msg,
                    icon:'none'
                })
            }
        })
    },
    getProviderDetail(){
        axios.post('/api/contact/detail',{
            id:this.data.id
        }).then(res=>{
            wx.hideLoading()
            if(res.code==1){
                res.data = res.data[0]
                this.setData({
                    typeIndex:res.data.type,
                    name:res.data.wechat,
                    qrcodeImage:res.data.qrcode,
                    remark:res.data.remark
                })
            }
        })
    }
})
